package com.idol.sql.session;

import com.idol.pojo.Configeration;

import java.lang.reflect.*;
import java.util.List;

/**
 * 封装具体的 SQL 查询方法
 *
 * @author Supreme_Sir
 * @version 1.0
 * @className DefaultSqlSession
 * @description
 * @date 2020/9/26 16:34
 **/
public class DefaultSqlSession implements SqlSession {
    private Configeration configeration;

    @Override
    public <E> List<E> selectAll(String statementID, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        return simpleExecutor.execute(this.configeration, this.configeration.getMappedStatement(statementID), params);
    }

    @Override
    public <T> T selectOne(String statementID, Object... params) throws Exception {
        List<T> objectList = selectAll(statementID, params);
        if (objectList.size() == 1) {
            return objectList.get(0);
        } else {
            throw new RuntimeException("查询结果为空或查询结果不唯一！！！");
        }
    }

    @Override
    public void insert(String statementID, Object... params) throws Exception {
        selectAll(statementID, params);
    }

    @Override
    public void delete(String statementID, Object... params) throws Exception {
        selectAll(statementID, params);
    }

    @Override
    public void modify(String statementID, Object... params) throws Exception {
        selectAll(statementID, params);
    }


    /**
     * 通过 Java 动态代理执行 SQL
     *
     * @param mapperClass
     * @param <T>
     * @return
     */
    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                // 由于代理对象无法获取 SQL 配置文件中的相关信息，因此通过类名和方法名的拼接，完成成对应 id SQL 的调用
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementID = className + "." + methodName;

                String sqlType = configeration.getSqlType(statementID);
                if (sqlType != null){
                    switch (sqlType) {
                        case "insert":
                            insert(statementID, args);
                            return null;
                        case "delete":
                            delete(statementID, args);
                            return null;
                        case "update":
                            modify(statementID, args);
                            return null;
                        default:
                            // 判断返回结果是否存在泛型，如果存在泛型则表示改结果为集合，则调用 selectAll 方法
                            Type returnType = method.getGenericReturnType();
                            if (returnType instanceof ParameterizedType) {
                                return selectAll(statementID, args);
                            }
                            return selectOne(statementID, args);
                    }
                }
                throw new RuntimeException("查询错误，请检查您的操作~！");
            }
        });
        return (T) proxyInstance;
    }


    public DefaultSqlSession(Configeration configeration) {
        this.configeration = configeration;
    }
}
